{% extends "common/layout_main.html" %}
{% block content %}
<div class="row  border-bottom">
    <div class="col-lg-12">
        <div class="tab_title">
            <ul class="nav nav-pills">
                <li class="current">
                    <a href="{{ url_for('food_page.index') }}">美食列表</a>
                </li>
                <li>
                    <a href="{{ url_for('food_page.category') }}">分类列表</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <form class="form-inline wrap_search">
            <div class="row  m-t p-w-m">
                <div class="form-group">
                    <select name="status" class="form-control inline">
                        <option value="-1">请选择状态</option>
                        <option value="1">正常</option>
                        <option value="0">已删除</option>
                    </select>
                </div>
                <div class="form-group">
                    <select name="category_id" class="form-control inline">
                        <option value="0">请选择分类</option>
                        {% for item in pagination.data_list %}
                            <option value="{{ item.category_id }}">{{ item.category.name}} </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="form-group">
                    <div class="input-group">
                        <button type="button" class="btn  btn-primary search">
                            <i class="fa fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
            </div>
            <hr>
            <div class="row">
                <div class="col-lg-12">
                    <a class="btn btn-w-m btn-outline btn-primary pull-right" href="{{ url_for('food_page.insert') }}">
                        <i class="fa fa-plus"></i> 美食
                    </a>
                </div>
            </div>

        </form>
        <table class="table table-bordered m-t">
            <thead>
            <tr>
                <th>序号</th>
                <th>美食名</th>
                <th>分类</th>
                <th>价格</th>
                <th>当日库存</th>
                <th>标签</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% if pagination.data_list %}
                {% for item in pagination.data_list %}
            <tr>
                <td>{{ loop.index }}</td>
                <td>{{ item.name }}</td>
                <td>{{ item.category.name  }}</td>
                <td>{{ item.price }}</td>
                <td>{{ item.stock }}</td>
                <td>{{ item.tags }}</td>

                <td>
                    <a href="{{ url_for('food_page.info') }}?id={{ item.id  }}">
                        <i class="fa fa-eye fa-lg"></i>
                    </a>
                     {%  if item.status == 1 %}
                    <a class="m-l" href="{{ url_for('food_page.edit') }}?id={{ item.id  }}">
                        <i class="fa fa-edit fa-lg"></i>
                    </a>

                    <a class="m-l remove" href="javascript:fun('remove',{{ item.id }},{{ pagination.current_page }},{{ condition.status }},{{ condition.category_id }});">
                        <i class="fa fa-trash fa-lg"></i>
                    </a>
                    {% else %}
                       <a class="m-l recover" href="javascript:fun('recover',{{ item.id }},{{ pagination.current_page }},{{ condition.status }},{{ condition.category_id }});">
                        <i class="fa fa-rotate-left fa-lg"></i>
                    </a>
                    {% endif %}
                </td>
            </tr>
             {% endfor %}
            {% else %}
                <tr><td colspan="7">暂无数据~~</td></tr>
            {% endif %}
            </tbody>
        </table>
        <!--分页代码-->
        <div class="row">
            <div class="col-lg-12">
                <span class="pagination_count" style="line-height: 40px;">共{{ pagination.totals }}条数据 | {{ pagination.total_pages }}页</span>
                <ul class="pagination pagination-sm no-margin pull-right">
                <li><a href="{{ url_for('food_page.index') }}?page={{ pagination.prev_page }}&status={{ condition.status }}&category_id={{ condition.category_id }}">&laquo;</a></li>
                {% if pagination.total_pages <= 15 %}
                    {% for k in pagination.page_range %}
                        {% if pagination.current_page == k %}
                        <li class="active"><a href="{{ url_for('food_page.index') }}?page={{ k }}&status={{ condition.status }}&category_id={{ condition.category_id }}">{{ k }}</a></li>
                        {% else %}
                        <li><a href="{{ url_for('food_page.index') }}?page={{ k }}&status={{ condition.status }}&category_id={{ condition.category_id }}">{{ k }}</a></li>
                        {% endif %}
                    {% endfor %}
                {% else %}
                        <li><a href="javascript:void(0);">...</a></li>
                {% endif %}
                <li><a href="{{ url_for('food_page.index') }}?page={{ pagination.next_page }}&status={{ condition.status }}&category_id={{ condition.category_id }}">&raquo;</a></li>
              </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}
{% block js %}
<script>
    var wrap_search = {
		init:function(){
			this.eventBind()
		},
		eventBind:function(){
            $(".wrap_search .search").click(function(){
                $(".wrap_search").submit()
            })
		}
	}

    $(document).ready(function(){
        wrap_search.init();
    })

    function fun(opt,id,page,status,category_id){
        var callback = {
            "ok":function(){
                $.ajax({
                    url:"/food/" + opt,
                    type:"POST",
                    data:{"id":id},
                    dataType:"json",
                    success:function(res){
                        var callback = null
                        if(res.code == 200){
                            callback = function(){
                                window.location.href = "/food/index?page="+page +"&status="+status+"&category_id="+category_id
                            }
                            common_ops.alert(res.msg,callback)
                        }

                        if(res.code == -1){
                            common_ops.alert(res.msg)
                        }
                    }
                })
            },
            "cancel":null
        };
        common_ops.confirm((opt=="remove" ? "确定删除吗?" : "确定恢复吗?"),callback)
    }
</script>

{% endblock %}